<template>
  <div>
    <el-dialog title="提示" :visible="isShow" width="50%" @close="closeEvent">
      <el-form label-width="120px" :model="form" :rules="rules" ref="form">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-date-picker
            v-model="form.timeOfEntry"
            type="date"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="聘用形势" prop="formOfEmployment">
          <el-select placeholder="请选择" v-model="form.formOfEmployment">
            <el-option
              v-for="item in employees.hireType"
              :key="item.id"
              :value="item.id"
              :label="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="form.workNumber"></el-input>
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <div ref="treeArea">
            <el-input
              v-model="form.departmentName"
              @focus="focusEvent"
              readonly
            >
            </el-input>
            <el-tree
              v-if="treeShow"
              :data="treeData"
              :props="{ label: 'name' }"
              @node-click="nodeClick"
            >
            </el-tree>
          </div>
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker
            v-model="form.correctionTime"
            type="date"
          ></el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button type="primary" @click="submit">提交</el-button>
          <el-button @click="closeEvent">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import employees from '@/assets/constant/employees'
import { companyDepartment } from '@/api/departments'
import { sysUserPost } from '@/api/employees'
export default {
  name: 'Dialog',
  props: {
    isShow: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      employees: employees,
      treeData: [],
      treeShow: false,
      form: {
        username: '', //	string	非必须		姓名
        mobile: '', //	string	非必须		手机号
        formOfEmployment: '', //	number	非必须		聘用形式
        workNumber: '', //	string	非必须		工号
        departmentName: '', //	string	非必须		组织名称
        timeOfEntry: '', //	string	非必须		入职时间
        correctionTime: '' //	string	非必须		转正时间
      },
      rules: {
        username: { required: true, message: '必填', trigger: 'change' }, //	string	非必须		姓名
        mobile: { required: true, message: '必填', trigger: 'change' }, //	string	非必须		手机号
        formOfEmployment: {
          required: true,
          message: '必填',
          trigger: 'change'
        }, //	number	非必须		聘用形式
        workNumber: { required: true, message: '必填', trigger: 'change' }, //	string	非必须		工号
        departmentName: { required: true, message: '必填', trigger: 'change' }, //	string	非必须		组织名称
        timeOfEntry: { required: true, message: '必填', trigger: 'change' }, //	string	非必须		入职时间
        correctionTime: { required: true, message: '必填', trigger: 'change' } //	string	非必须		转正时间
      }
    }
  },
  mounted () {
    document.addEventListener('click', this.clickEvent)
  },

  methods: {
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          await sysUserPost(this.form)
          this.$message.success('添加成功')
          this.$emit('getData')
          this.isShow=false
          console.log(result)
        }
      })
    },
    closeEvent () {
      this.$emit('update:isShow', false)
    },
    async focusEvent () {
      this.treeShow = true
      const res = await companyDepartment()
      this.treeData = this.changeData(res.data.depts)
      console.log('什么数据', res)
    },
    changeData (arr, str = '') {
      return arr.filter(item => {
        if (item.pid === str) {
          item.children = this.changeData(arr, item.id)
          return true
        }
      })
    },
    nodeClick (data) {
      this.form.departmentName = data.name
    },
    clickEvent (e) {
      if (this.isShow === true && !this.$refs.treeArea.contains(e.target)) {
        this.treeShow = false
      }
    }
  },
  beforeDestroy () {
    document.removeEventListener('click', this.clickEvent)
  }
}
</script>

<style></style>
